﻿    using WebApp.BLL.Base;
using WebApp.Common.Caching;
using WebApp.DAL;
using WebApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using WebApp.ViewModels.Manage;

namespace WebApp.BLL.Manage
{
    /// <summary>
    /// 系统参数
    /// </summary>
    public class S_lang_resService : BaseService<S_lang_res>
    {
        #region (DAL)Repository
        
        #endregion

        #region 构造

        public S_lang_resService() : base(new S_lang_resRepository()) { }

        #endregion

        #region PageList

        public List<S_lang_res> PageList(int pageindex, int pagesize, string searchkey, string lang, string rescategory, string sort, string order, out int rowscount)
        {
            var T = this.CurrentRepository.Entities;

            //查询条件
            if (!string.IsNullOrEmpty(rescategory))
            {
                T = T.Where(p => p.Rescategory == rescategory);
            }
            if (!string.IsNullOrEmpty(lang))
            {
                T = T.Where(p => p.Lang == lang);
            }
            if (!string.IsNullOrEmpty(searchkey))
            {
                T = T.Where(p => p.Reskey.Contains(searchkey) || p.Resname.Contains(searchkey) || p.Rescontent.Contains(searchkey));
            }

            //根据属性名实现动态排序
            if (string.IsNullOrEmpty(sort))
            {
                T = T.OrderBy(p => p.Rescategory).ThenBy(p => p.Reskey);
            }
            else
            {
                T = base.SortList(T, sort, order == "asc" ? true : false);
            }

            //返回记录数
            rowscount = T.Count();
            //返回分页结果
            return this.CurrentRepository.PageList(T, pageindex, pagesize).ToList();
        }

        #endregion

        #region ListByLang

        public List<S_lang_res> ListByLang(string lang)
        {
            return this.CurrentRepository.Entities.Where(p => p.Lang == lang)
                .OrderBy(p => p.Rescategory)
                .ThenBy(p => p.Reskey)
                .ToList();
        }

        #endregion

        #region ListByReskey

        public List<S_lang_res> ListByReskey(string reskey)
        {
            return this.CurrentRepository.Entities.Where(p => p.Reskey == reskey).OrderBy(p => p.Lang).ToList();
        }

        #endregion

        #region Save

        public bool Save(LangResEditModel model)
        {
            //key去空格
            model.Reskey = model.Reskey.Trim();

            foreach (var item in model.ContentList)
            {
                var check = this.CurrentRepository.Entities.Where(p => p.Lang == item.Lang && p.Reskey == model.Reskey).FirstOrDefault();
                if (check == null)
                {
                    var lang = new S_lang_res
                    {
                        Lang = item.Lang,
                        Resname = model.Resname,
                        Rescategory = model.Rescategory,
                        Reskey = model.Reskey,
                        Resjs = model.Resjs,
                        Rescontent = item.Content
                    };
                    this.CurrentRepository.Add(lang);
                }
                else
                {
                    check.Resname = model.Resname;
                    check.Rescategory = model.Rescategory;
                    check.Resjs = model.Resjs;
                    check.Rescontent = item.Content;
                    this.CurrentRepository.Update(check);
                }
            }

            return true;
        }

        #endregion

        #region 删除

        public bool DeleteByIds(string ids)
        {
            foreach (string id in ids.Split(','))
            {
                if (string.IsNullOrEmpty(id)) continue;

                var item = this.CurrentRepository.Find(Convert.ToInt32(id));

                var list = this.CurrentRepository.Entities.Where(p => p.Reskey == item.Reskey);
                foreach (var o in list)
                {
                    this.CurrentRepository.Delete(o, false);
                }
            }

            this.CurrentRepository.Save();

            return true;
        }

        #endregion

        #region Exist

        public bool Exist(int id, string reskey)
        {
            var T = this.CurrentRepository.Entities;

            if (id > 0)
            {
                var item = this.Find(id);
                var list = this.CurrentRepository.Entities.Where(p => p.Reskey == item.Reskey).Select(p => p.Id).ToList();
                T = T.Where(p => !list.Contains(p.Id));
            }

            T = T.Where(p => p.Reskey == reskey);

            return T.Any();
        }

        #endregion

        #region DistinctRescategory

        public List<string> DistinctRescategory()
        {
            return this.CurrentRepository.Entities.Select(p => p.Rescategory).Distinct().ToList();
        }

        #endregion

        #region GetReskeyByName

        public string GetReskeyByName(string resname)
        {
            var item = this.CurrentRepository.Entities.FirstOrDefault(p => p.Resname == resname);

            return item == null ? "" : item.Reskey;
        }

        #endregion

        #region GetValue

        /// <summary>
        /// 根据Key获取系统参数值
        /// </summary>
        /// <param name="paramname"></param>
        /// <param name="custcode"></param>
        /// <returns></returns>
        public string GetValue(string key, string lang = "zh_cn")
        {
            var cachekey = "S_lang_res";

            if (string.IsNullOrEmpty(lang))
            {
                lang = "zh_cn";
            }

            var result = new MemoryCacheManager().Get<List<S_lang_res>>(cachekey, () =>
            {
                return this.CurrentRepository.Entities.ToList();
            });

            var T = result.Where(p => p.Lang == lang && p.Reskey.Trim() == key.Trim());

            var item = T.FirstOrDefault();

            return item == null ? key : item.Rescontent;
        }

        public void ClearCache()
        {
            var cachekey = "S_lang_res";

            new MemoryCacheManager().Remove(cachekey);
        }

        #endregion

    }
}
